package cn.springmvc.controller;
 
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.springmvc.model.ResultBaseBean;
import cn.springmvc.model.entity.AlbumBean;
import cn.springmvc.service.IAlbumService;
import cn.springmvc.util.SessionManager;
 
@Controller
@RequestMapping(value="/rest")
public class AlbumController{
	private static final Logger logger = Logger.getLogger(AlbumController.class);
	
	@Autowired
	private IAlbumService albumService;
	
    @RequestMapping(value="/album", method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<AlbumBean> addAlbum(@RequestBody AlbumBean albumBean,@RequestParam("sessionId") String sessionId){
    	ResultBaseBean<AlbumBean> resultBaseBean = albumService.insert(albumBean);
    	
    	SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			
	    	resultBaseBean = albumService.insert(albumBean);
		}else{
			resultBaseBean = new ResultBaseBean<AlbumBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
    	
    	return resultBaseBean;
    }
    
    
    @RequestMapping(value = "/album/{id}",method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<AlbumBean> getOneAlbum(@PathVariable Integer id,@RequestParam("sessionId") String sessionId){
    	ResultBaseBean<AlbumBean> resultBaseBean;
    	
    	SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			AlbumBean albumBean = new AlbumBean();
	    	albumBean.setAlbumId(id);
	    	resultBaseBean = albumService.selectOne(albumBean);
		}else{
			resultBaseBean = new ResultBaseBean<AlbumBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
	
    	return resultBaseBean;
    }
    
    
    @RequestMapping(value="/album",method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<AlbumBean> getAlbumList(@RequestParam("sessionId") String sessionId){
    	ResultBaseBean<AlbumBean> resultBaseBean;
    	
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			AlbumBean albumBean = new AlbumBean();
			resultBaseBean = albumService.select(albumBean);
		}else{
			resultBaseBean = new ResultBaseBean<AlbumBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
	
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/album",method=RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<AlbumBean> updateAlbum(@RequestBody AlbumBean albumBean,@RequestParam("sessionId") String sessionId){
    	logger.info("Update album with id: " + albumBean.getAlbumId());
    	
    	ResultBaseBean<AlbumBean> resultBaseBean;
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			resultBaseBean = albumService.update(albumBean);
		}else{
			resultBaseBean = new ResultBaseBean<AlbumBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
		
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/album",method=RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<AlbumBean> deleteAlbum(@RequestBody AlbumBean albumBean, @RequestParam("sessionId") String sessionId){
    	
    	ResultBaseBean<AlbumBean> resultBaseBean;
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			resultBaseBean = albumService.delete(albumBean);
		}else{
			resultBaseBean = new ResultBaseBean<AlbumBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
    	
    	return resultBaseBean;
    }
     
}